package com.fanbai.score.user.http;

import android.content.Context;

import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.common.http.HttpUtils;
import com.fanbai.score.common.http.WebCodes;
import com.fanbai.score.user.bean.ActInfo_Bean;
import com.fanbai.score.user.bean.ShopClass_Bean;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * Created by acer on 2017/6/3.
 */

public class UserActivity_RequestNet_Utils {
    /**
     * 获取分类信息
     * @param ctx
     * @param callBack
     */
    public static void getActClassInfos(final Context ctx, final CallBack<List<ShopClass_Bean>> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getactclassinfos");
        String url = WebCodes.HTTP_URL_USER_ACTIVITY ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    List<ShopClass_Bean> beans = JsonUtils.toArr(body, ShopClass_Bean[].class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, beans);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 获取活动列表
     * @param ctx
     * @param clsid
     * @param callBack
     */
    public static void getActivityInfosByClsid(final Context ctx, String clsid, final CallBack<ArrayList<ActInfo_Bean>> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getactivityinfosbyclsid");
        params.put("clsid", clsid);
        String url = WebCodes.HTTP_URL_USER_ACTIVITY ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    List<ActInfo_Bean> beans = JsonUtils.toArr(body, ActInfo_Bean[].class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, new ArrayList<ActInfo_Bean>(beans));
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }
}
